/// @author  Alberto Franco
/// @file    medium.go
/// @date    05-05-2011
/// @brief   Contains the definition of the medium process. 
package netsim

import (
    "rand"
)

/// The medium structure represent transmission medium
type Medium struct {
    sendChan  *chan int32
    errorChan chan int32
    transChan  chan int32
}

/// Create a new TCP simulator
/// @return Newly created simulator.
func CreateMedium() *Medium {
    var medium *Medium = new (Medium)
    medium.errorChan = make (chan int32)
    medium.transChan = make (chan int32)
    return medium
}

/// The main method for medium. In CCS:
/// Medium     = send(x).Sending(x)
/// Sending(x) = tau.Error + 'trans(x).Medium
/// Error      = error.Medium
func (self *Medium) Serve() {
    var packet int32
    for {
        /// Receive a packet
        packet = <- (*self.sendChan)
        if rand.Intn(100) > 50 {
            /// Generate random errors
            self.errorChan <- 1
        } else {
            self.transChan <- packet
        }
    }
}

